package IU;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import GEF.Aplicacao;

import java.awt.Component;

@SuppressWarnings("serial")
public class Login extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField tFNomeDeUtilizador;
	private JPasswordField passwordField;
	private JButton cancelButton;
	private JLabel lblNomeDeUtilizador;
	private JLabel lblPassword;
	private JButton btnLogin;
	private Janela janela;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		try {
//			Login dialog = new Login();
//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	/**
	 * Create the dialog.
	 */
	public Login(Aplicacao app, Janela j) {
		this.janela=j;
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				super.windowClosing(e);
				System.exit(0);
			}
		});
		setModal(true);
		setResizable(false);
		setTitle("Autenticar");
		setBounds(100, 100, 256, 162);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel);
			panel.setLayout(null);
			
			lblNomeDeUtilizador = new JLabel("Nome de utilizador:");
			lblNomeDeUtilizador.setBounds(10, 11, 112, 21);
			panel.add(lblNomeDeUtilizador);
			
			tFNomeDeUtilizador = new JTextField(5);
			tFNomeDeUtilizador.setToolTipText("");
			tFNomeDeUtilizador.setBounds(128, 12, 86, 20);
			panel.add(tFNomeDeUtilizador);
			tFNomeDeUtilizador.setColumns(10);
			{
				lblPassword = new JLabel("Password:");
				lblPassword.setBounds(10, 43, 112, 21);
				panel.add(lblPassword);
			}
			
			passwordField = new JPasswordField();
			passwordField.setBounds(128, 43, 86, 20);
			panel.add(passwordField);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				cancelButton = new JButton("Cancelar");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						System.exit(0);
					}
				});
				{
					btnLogin = new JButton("Atenticar");
					btnLogin.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							if(app.fazLogin(tFNomeDeUtilizador.getText(), passwordField.getPassword())){
								dispose();
								}
							else{
								JOptionPane.showMessageDialog(janela,
									    janela.getERRORMSG(),
									    "ERRO",
									    JOptionPane.ERROR_MESSAGE);
							}
								
						}
					});
					buttonPane.add(btnLogin);
				}
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{tFNomeDeUtilizador, passwordField, cancelButton, lblNomeDeUtilizador, lblPassword}));
	}
}
